<?php

session_start();

if (!isset($_SESSION['user'])) {
    header("Location: http://www.salesianichioggia.it/dashboard/login.php");
    die();
}

require_once ('../classes/HTMLManager.class.php');

$u = unserialize($_SESSION['user']);

if ((!in_array(1, $u->permissions) && !in_array(9, $u->permissions))) {
    echo'Non hai il permesso di accedere a questa pagina! Contatta il webmaster per ulteriori informazioni';
    die();
}

define(ACTION_NEW, 'new');
define(ACTION_EDIT, 'edit');
define(ACTION_LIST, 'list');
define(ACTION_SAVE, 'save');
define(ACTION_DELETE, 'delete');


require_once ('../classes/GalleryManager.class.php');

if (isset($_GET['action'])) {
    $gm = new GalleryManager();
    if ($_GET['action'] == ACTION_LIST) {
        $year = (isset($_GET['year'])) ? $_GET['year'] : date('Y');
        if (in_array(1, $u->permissions)) {
            $galleries = $gm->get_galleries_by_year($year);
        } else {
            $galleries = $gm->get_galleries_by_year_by_creator($year, $u->id);
        }
    } else if ($_GET['action'] == ACTION_EDIT) {
        if (isset($_GET['id'])) {
            $gallery = $gm->get_gallery_by_id($_GET['id']);
            $gm->get_photos($gallery);
        } else {
            die('Il documento ricercato non esiste!');
        }
    } else if ($_GET['action'] == ACTION_SAVE) {
       if(isset($_GET['id'])) {
           $gallery = $gm->get_gallery_by_id($_GET['id']);
           $gallery->title = $_POST['title'];
           $gallery->description = $_POST['description'];
           $gallery->folder = $_POST['folder'];
           $gm->update_gallery($gallery);
           header('Location: http://www.salesianichioggia.it/dashboard/gallery.php?action=list');
           die();
       } else {
           if (isset($_POST['gallery_id'])) {
               $gm->show_gallery($_POST['gallery_id']);
               header('Location: http://www.salesianichioggia.it/dashboard/gallery.php?action=list');
               die();
           }
       }
    } else if ($_GET['action'] == ACTION_DELETE) {
        if (isset($_GET['id'])) {
            $gm->delete_gallery($_GET['id']);
            header('Location: http://www.salesianichioggia.it/dashboard/gallery.php?action=list');
        }
        
    }
}


include_once ('gallery.view.php');
?>
